package com.weiyuwqj.admin.controller.admin.picture;

import com.weiyuwqj.admin.controller.admin.log.annotation.LogAnnotation;
import com.weiyuwqj.api.service.SysPictureService;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.model.SysPicture;
import com.weiyuwqj.api.util.IpUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/12/7.
 */
@Controller
@RequestMapping(value="/admin/picture")
public class SysPictureController {
    @Autowired
    SysPictureService sysPictureService;
    //显示
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(Paginator paginator,HttpServletRequest request){
        String name = request.getParameter("name");
        String pictureType = request.getParameter("pictureType");
        String beginDateTime = request.getParameter("beginDateTime");
        String endDateTime = request.getParameter("endDateTime");
        Map<String, Object> map = new HashMap<String, Object>();
        if(StringUtils.isNotEmpty(name)){
            map.put("name", name);
        }
        if(StringUtils.isNotEmpty(pictureType)){
            map.put("pictureType", pictureType);
        }
        if(StringUtils.isNotEmpty(beginDateTime)){
            map.put("beginDateTime", beginDateTime);
        }
        if(StringUtils.isNotEmpty(endDateTime)){
            map.put("endDateTime", endDateTime);
        }
        return sysPictureService.getList(paginator, map);
    }

    //增加
    @ResponseBody
    @RequestMapping("/add")
    @LogAnnotation(module ="图片管理",description = "增加")
    public Object add(SysPicture picture){
        picture.setAddtime(new Date());
        picture.setAddip(IpUtils.get());
        sysPictureService.save(picture);
        return Message.OK;
    }
    //获取编辑信息
    @ResponseBody
    @RequestMapping(value = "/getSingle",method = RequestMethod.POST)
    public Object getSingle(Integer id) {
        return sysPictureService.getSingle(id);
    }
    //编辑
    @ResponseBody
    @RequestMapping(value="/edit",method = RequestMethod.POST)
    @LogAnnotation(module ="图片管理",description = "编辑")
    public Object edit(SysPicture picture,String pics){
        if(pics != null && !pics.equals("")){
            picture.setPicturePath(pics);
        }
        sysPictureService.updateNotNull(picture);
        return Message.OK;
    }
    //获取图片地址
   @ResponseBody
    @RequestMapping(value="/getPictureByItemId",method = RequestMethod.POST)
    public Object getPictureByItemId(Integer itemId){
       Map map = new HashMap();
       map.put("itemId",itemId);
        return sysPictureService.getPictureByItemId(map);
   }

    //首页轮播图片
    @ResponseBody
    @RequestMapping(value = "/mainList", method = RequestMethod.POST)
    public Object mainList(Integer type){
        return sysPictureService.getMainPicture(type);
    }

    //首页合作伙伴图片
    @ResponseBody
    @RequestMapping(value = "/partnerPicture", method = RequestMethod.POST)
    public Object PartnerPicture(){
        //List<SysPicture> list=
        return sysPictureService.getPartnerPicture();
    }

    //项目图片
    @ResponseBody
    @RequestMapping(value = "/itemPicture", method = RequestMethod.POST)
    public Object itemPicture(Paginator paginator,HttpServletRequest request){
        String itemId = request.getParameter("itemId");
        Map<String, Object> map = new HashMap<String, Object>();
        if(StringUtils.isNotEmpty(itemId)){
            map.put("itemId", itemId);
        }
        return sysPictureService.getListByItemPicture(paginator, map);
    }

    @ResponseBody
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @LogAnnotation(module ="图片管理",description = "删除图片")
    public Object deleteById(Integer id){
        sysPictureService.delete(id);
        return Message.OK;
    }

    //根据id修改项目图片类型
    @ResponseBody
    @RequestMapping(value = "/modifyItemPictureTypeById", method = RequestMethod.POST)
    @LogAnnotation(module ="图片管理",description = "修改图片类型")
    public Object modifyItemPictureTypeById(Integer itemPictureType,Integer id){
        sysPictureService.modifyItemPictureTypeById(itemPictureType,id);
        return Message.OK;
    }
}

